﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Bangle.DBUtility;
namespace Bangle.DAL
{
    //T_Customer
    public partial class T_CustomerDAL
    {

        public bool Exists(Guid CustomerId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Customer");
            strSql.Append(" where ");
            strSql.Append(" CustomerId = @CustomerId  ");
            SqlParameter[] parameters = {
					new SqlParameter("@CustomerId", SqlDbType.UniqueIdentifier,16)			
            };
            parameters[0].Value = CustomerId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Bangle.Model.T_Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Customer(");
            strSql.Append("CustomerId,CustomerCode,CustomerName,Country,Email,Phone,CompanyName,Skype,Whatsapp,QQ,Wechat,Webside,Adress,ProductDescription,CustomerReview,TargetMarket,Schedule,CreateTime,CreateUserId,UpdateTime,UpdateUserId,Valid,CustomerTypeId,Gender,Wangwang");
            strSql.Append(") values (");
            strSql.Append("@CustomerId,@CustomerCode,@CustomerName,@Country,@Email,@Phone,@CompanyName,@Skype,@Whatsapp,@QQ,@Wechat,@Webside,@Adress,@ProductDescription,@CustomerReview,@TargetMarket,@Schedule,@CreateTime,@CreateUserId,@UpdateTime,@UpdateUserId,@Valid,@CustomerTypeId,@Gender,@Wangwang");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			            new SqlParameter("@CustomerId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@CustomerCode", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@CustomerName", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Country", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@Email", SqlDbType.VarChar,500) ,            
                        new SqlParameter("@Phone", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@CompanyName", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Skype", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@Whatsapp", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@QQ", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@Wechat", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@Webside", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Adress", SqlDbType.VarChar,1000) ,            
                        new SqlParameter("@ProductDescription", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@CustomerReview", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@TargetMarket", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@Schedule", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4) ,            
                        new SqlParameter("@CustomerTypeId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Gender", SqlDbType.Int,4) ,            
                        new SqlParameter("@Wangwang", SqlDbType.VarChar,100)             
              
            };

            parameters[0].Value = model.CustomerId;
            parameters[1].Value = model.CustomerCode;
            parameters[2].Value = model.CustomerName;
            parameters[3].Value = model.Country;
            parameters[4].Value = model.Email;
            parameters[5].Value = model.Phone;
            parameters[6].Value = model.CompanyName;
            parameters[7].Value = model.Skype;
            parameters[8].Value = model.Whatsapp;
            parameters[9].Value = model.QQ;
            parameters[10].Value = model.Wechat;
            parameters[11].Value = model.Webside;
            parameters[12].Value = model.Adress;
            parameters[13].Value = model.ProductDescription;
            parameters[14].Value = model.CustomerReview;
            parameters[15].Value = model.TargetMarket;
            parameters[16].Value = model.Schedule;
            parameters[17].Value = model.CreateTime;
            parameters[18].Value = model.CreateUserId;
            parameters[19].Value = model.UpdateTime;
            parameters[20].Value = model.UpdateUserId;
            parameters[21].Value = model.Valid;
            parameters[22].Value = model.CustomerTypeId;
            parameters[23].Value = model.Gender;
            parameters[24].Value = model.Wangwang;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Bangle.Model.T_Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Customer set ");

            strSql.Append(" CustomerId = @CustomerId , ");
            strSql.Append(" CustomerCode = @CustomerCode , ");
            strSql.Append(" CustomerName = @CustomerName , ");
            strSql.Append(" Country = @Country , ");
            strSql.Append(" Email = @Email , ");
            strSql.Append(" Phone = @Phone , ");
            strSql.Append(" CompanyName = @CompanyName , ");
            strSql.Append(" Skype = @Skype , ");
            strSql.Append(" Whatsapp = @Whatsapp , ");
            strSql.Append(" QQ = @QQ , ");
            strSql.Append(" Wechat = @Wechat , ");
            strSql.Append(" Webside = @Webside , ");
            strSql.Append(" Adress = @Adress , ");
            strSql.Append(" ProductDescription = @ProductDescription , ");
            strSql.Append(" CustomerReview = @CustomerReview , ");
            strSql.Append(" TargetMarket = @TargetMarket , ");
            strSql.Append(" Schedule = @Schedule , ");
            strSql.Append(" CreateTime = @CreateTime , ");
            strSql.Append(" CreateUserId = @CreateUserId , ");
            strSql.Append(" UpdateTime = @UpdateTime , ");
            strSql.Append(" UpdateUserId = @UpdateUserId , ");
            strSql.Append(" Valid = @Valid , ");
            strSql.Append(" CustomerTypeId = @CustomerTypeId , ");
            strSql.Append(" Gender = @Gender , ");
            strSql.Append(" Wangwang = @Wangwang  ");
            strSql.Append(" where CustomerId=@CustomerId  ");

            SqlParameter[] parameters = {
			            new SqlParameter("@CustomerId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@CustomerCode", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@CustomerName", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Country", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@Email", SqlDbType.VarChar,500) ,            
                        new SqlParameter("@Phone", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@CompanyName", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Skype", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@Whatsapp", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@QQ", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@Wechat", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@Webside", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Adress", SqlDbType.VarChar,1000) ,            
                        new SqlParameter("@ProductDescription", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@CustomerReview", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@TargetMarket", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@Schedule", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4) ,            
                        new SqlParameter("@CustomerTypeId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Gender", SqlDbType.Int,4) ,            
                        new SqlParameter("@Wangwang", SqlDbType.VarChar,100)             
              
            };

            parameters[0].Value = model.CustomerId;
            parameters[1].Value = model.CustomerCode;
            parameters[2].Value = model.CustomerName;
            parameters[3].Value = model.Country;
            parameters[4].Value = model.Email;
            parameters[5].Value = model.Phone;
            parameters[6].Value = model.CompanyName;
            parameters[7].Value = model.Skype;
            parameters[8].Value = model.Whatsapp;
            parameters[9].Value = model.QQ;
            parameters[10].Value = model.Wechat;
            parameters[11].Value = model.Webside;
            parameters[12].Value = model.Adress;
            parameters[13].Value = model.ProductDescription;
            parameters[14].Value = model.CustomerReview;
            parameters[15].Value = model.TargetMarket;
            parameters[16].Value = model.Schedule;
            parameters[17].Value = model.CreateTime;
            parameters[18].Value = model.CreateUserId;
            parameters[19].Value = model.UpdateTime;
            parameters[20].Value = model.UpdateUserId;
            parameters[21].Value = model.Valid;
            parameters[22].Value = model.CustomerTypeId;
            parameters[23].Value = model.Gender;
            parameters[24].Value = model.Wangwang;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid CustomerId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Customer ");
            strSql.Append(" where CustomerId=@CustomerId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CustomerId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = CustomerId;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Bangle.Model.T_Customer GetModel(Guid CustomerId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CustomerId, CustomerCode, CustomerName, Country, Email, Phone, CompanyName, Skype, Whatsapp, QQ, Wechat, Webside, Adress, ProductDescription, CustomerReview, TargetMarket, Schedule, CreateTime, CreateUserId, UpdateTime, UpdateUserId, Valid, CustomerTypeId, Gender, Wangwang  ");
            strSql.Append("  from T_Customer ");
            strSql.Append(" where CustomerId=@CustomerId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CustomerId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = CustomerId;


            Bangle.Model.T_Customer model = new Bangle.Model.T_Customer();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CustomerId"].ToString() != "")
                {
                    model.CustomerId = Guid.Parse(ds.Tables[0].Rows[0]["CustomerId"].ToString());
                }
                model.CustomerCode = ds.Tables[0].Rows[0]["CustomerCode"].ToString();
                model.CustomerName = ds.Tables[0].Rows[0]["CustomerName"].ToString();
                model.Country = ds.Tables[0].Rows[0]["Country"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.CompanyName = ds.Tables[0].Rows[0]["CompanyName"].ToString();
                model.Skype = ds.Tables[0].Rows[0]["Skype"].ToString();
                model.Whatsapp = ds.Tables[0].Rows[0]["Whatsapp"].ToString();
                model.QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                model.Wechat = ds.Tables[0].Rows[0]["Wechat"].ToString();
                model.Webside = ds.Tables[0].Rows[0]["Webside"].ToString();
                model.Adress = ds.Tables[0].Rows[0]["Adress"].ToString();
                model.ProductDescription = ds.Tables[0].Rows[0]["ProductDescription"].ToString();
                model.CustomerReview = ds.Tables[0].Rows[0]["CustomerReview"].ToString();
                model.TargetMarket = ds.Tables[0].Rows[0]["TargetMarket"].ToString();
                model.Schedule = ds.Tables[0].Rows[0]["Schedule"].ToString();
                model.CreateTime = ds.Tables[0].Rows[0]["CreateTime"].ToString();
                if (ds.Tables[0].Rows[0]["CreateUserId"].ToString() != "")
                {
                    model.CreateUserId = int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
                }
                model.UpdateTime = ds.Tables[0].Rows[0]["UpdateTime"].ToString();
                if (ds.Tables[0].Rows[0]["UpdateUserId"].ToString() != "")
                {
                    model.UpdateUserId = int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Valid"].ToString() != "")
                {
                    model.Valid = int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CustomerTypeId"].ToString() != "")
                {
                    model.CustomerTypeId = int.Parse(ds.Tables[0].Rows[0]["CustomerTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Gender"].ToString() != "")
                {
                    model.Gender = int.Parse(ds.Tables[0].Rows[0]["Gender"].ToString());
                }
                model.Wangwang = ds.Tables[0].Rows[0]["Wangwang"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM T_Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.MenuId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Customer T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

